//
//  MyDiscountViewController.m
//  Findif
//
//  Created by Mac on 2019/4/2.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "MyDiscountViewController.h"
#import "MyDiscountTableViewCell.h"
#import "MyDiscountHeaderView.h"
#import "SureUserCouponModel.h"
#import "ManViewController.h"
#import "ZheViewController.h"
#import "ShopDetailsViewController.h"
#import "ExchangeViewController.h"

@interface MyDiscountViewController ()<UITableViewDelegate,UITableViewDataSource,MyDiscountHeaderViewDelegate,MyDiscountTableViewCellDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <SureUserCouponModel *> * modelArray;
//满减券
@property (nonatomic, strong)NSMutableArray <SureUserCouponModel *> * manArray;
//折扣券
@property (nonatomic, strong)NSMutableArray <SureUserCouponModel *> * zheArray;

@end

@implementation MyDiscountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRWhiteColor;
    self.manArray = [NSMutableArray array];
    self.zheArray = [NSMutableArray array];
    [self setUpNav];
    [self loadData];
    [self setupUI];
}


- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getUserCouponsResultWithMerchants_id:@"" WithBlock:^(BOOL success, id responseData) {
        
//        NSLog(@"res==%@",responseData);
        if (success) {
            
            self.modelArray = [NSArray yy_modelArrayWithClass:[SureUserCouponModel class] json:responseData[@"data"]];
        }
    }];
}


- (void)setModelArray:(NSArray<SureUserCouponModel *> *)modelArray{
    
    _modelArray = modelArray;
    
    for (SureUserCouponModel * model in modelArray) {
        
        int type = [model.merchants_coupons_type intValue];
        
        if (type == 1) {
            //满减券
            [self.manArray addObject:model];
            
        }else if (type == 2){
            //折扣券
            [self.zheArray addObject:model];
        }
    }
    
    [self.tableView reloadData];
}


- (void)setUpNav{
    
    self.title = @"我的优惠券";
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70 * SCREENH6, 15 * SCREENH6)];
    
    [rightButton setTitle:@"领券中心" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(pushToDiscountCenter) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];

    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
}

- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;
//    tableView.rowHeight = 110 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    
    [self.tableView registerClass:[MyDiscountTableViewCell class] forCellReuseIdentifier:@"MyDiscountCell"];
    [self.tableView registerClass:[MyDiscountHeaderView class] forHeaderFooterViewReuseIdentifier:@"MyDiscountView"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//        [self updateData];
//    }];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.manArray.count > 0 && self.zheArray.count > 0) {
        
        return 2;
    }else if(self.manArray.count > 0 || self.zheArray.count > 0){
        
        return 1;
    }else{
        
        return 0;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.manArray.count > 0 && self.zheArray.count > 0) {
        
        if (section == 0) {
            
            if (self.manArray.count <= 2) {
                
                return self.manArray.count;
            }else{
                
                return 2;
            }
            
        }else{
            
            if (self.zheArray.count <= 2) {
                
                return self.zheArray.count;
            }else{
                
                return 2;
            }
        }
        
    }else if(self.manArray.count > 0){
        
        if (self.manArray.count <= 2) {
            
            return self.manArray.count;
        }else{
            
            return 2;
        }
    }else{
        
        if (self.zheArray.count <= 2) {
            
            return self.zheArray.count;
        }else{
            
            return 2;
        }
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyDiscountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyDiscountCell" forIndexPath:indexPath];
    
    cell.delegate = self;
    
    if (self.manArray.count > 0 && self.zheArray.count > 0) {
        
        if (indexPath.section == 0) {
            
            cell.userModel = self.manArray[indexPath.row];
        }else{
            
            cell.userModel = self.zheArray[indexPath.row];
        }
        
    }else if(self.manArray.count > 0){
        
        cell.userModel = self.manArray[indexPath.row];
    }else{
        
        cell.userModel = self.zheArray[indexPath.row];
    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    MyDiscountHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyDiscountView"];
    
    header.delegate = self;
    
    if (self.manArray.count > 0 && self.zheArray.count > 0) {
        
        if (section == 0) {
            
            header.titleLabel.text = @"满减券";
        }else{
            
            header.titleLabel.text = @"折扣券";
        }
        
    }else if(self.manArray.count > 0){
        
        header.titleLabel.text = @"满减券";
    }else{
        
        header.titleLabel.text = @"折扣券";
    }

    return header;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 使用缓存行高，避免计算多次
    if (self.manArray.count > 0 && self.zheArray.count > 0) {
        
        if (indexPath.section == 0) {
            
            SureUserCouponModel * model = self.manArray[indexPath.row];
            return model.rowHeight;
        }else{

            SureUserCouponModel * model = self.zheArray[indexPath.row];
            return model.rowHeight;
        }
        
    }else if(self.manArray.count > 0){
        
        SureUserCouponModel * model = self.manArray[indexPath.row];
        return model.rowHeight;
    }else{
        
        SureUserCouponModel * model = self.zheArray[indexPath.row];
        return model.rowHeight;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    
    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ZRWhiteColor;
    }
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    NSString * shop_id;
//    if (self.manArray.count > 0 && self.zheArray.count > 0) {
//
//        if (indexPath.section == 0) {
//
//            SureUserCouponModel * model = self.manArray[indexPath.row];
//            shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
//
//        }else{
//
//            SureUserCouponModel * model = self.zheArray[indexPath.row];
//            shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
//        }
//
//    }else if(self.manArray.count > 0){
//
//        SureUserCouponModel * model = self.manArray[indexPath.row];
//        shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
//    }else{
//
//        SureUserCouponModel * model = self.zheArray[indexPath.row];
//        shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
//    }
//
//
//    if ([shop_id isEqualToString:@"0"]) {
//
//        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
//                                                                       message:@"该券为全场通用券，在任何一家店铺都能使用"
//                                                                preferredStyle:UIAlertControllerStyleAlert];
//
//        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
//                                                              handler:^(UIAlertAction * action) {
//
//                                                              }];
//
//        [alert addAction:defaultAction];
//        [self presentViewController:alert animated:YES completion:nil];
//    }else{
//
//        HiddenBar;
//        ShopDetailsViewController * detailsVC = [ShopDetailsViewController new];
//        detailsVC.ID = shop_id;
//        [self.navigationController pushViewController:detailsVC animated:YES];
//        ShowBar;
//    }
//
//}


- (void)pushToDiscountCenter{
    
    HiddenBar;
    
    ExchangeViewController * exchangeVC = [ExchangeViewController new];
    
    [self.navigationController pushViewController:exchangeVC animated:YES];
}


- (void)clickMoreButton:(NSString *)type{
    
    HiddenBar;
    if ([type isEqualToString:@"满减券"]) {
        
        ManViewController * manVC = [ManViewController new];
        
        manVC.modelArray = self.manArray;
        
        [self.navigationController pushViewController:manVC animated:YES];
        
    }else if ([type isEqualToString:@"折扣券"]){
        
        ZheViewController * manVC = [ZheViewController new];
        
        manVC.modelArray = self.zheArray;
        
        [self.navigationController pushViewController:manVC animated:YES];
        
    }
}

- (void)clickUnfoldButton{
    
    
    [self.tableView reloadData];
}

- (void)clickUseButtonWithCell:(MyDiscountTableViewCell *)cell{
    
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    
    NSString * shop_id;
    if (self.manArray.count > 0 && self.zheArray.count > 0) {
        
        if (indexPath.section == 0) {
            
            SureUserCouponModel * model = self.manArray[indexPath.row];
            shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
            
        }else{
            
            SureUserCouponModel * model = self.zheArray[indexPath.row];
            shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
        }
        
    }else if(self.manArray.count > 0){
        
        SureUserCouponModel * model = self.manArray[indexPath.row];
        shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
    }else{
        
        SureUserCouponModel * model = self.zheArray[indexPath.row];
        shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
    }
    
    
    if ([shop_id isEqualToString:@"0"]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"该券为全场通用券，在任何一家店铺都能使用"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        HiddenBar;
        ShopDetailsViewController * detailsVC = [ShopDetailsViewController new];
        detailsVC.ID = shop_id;
        [self.navigationController pushViewController:detailsVC animated:YES];
        ShowBar;
    }

}

@end
